package com.github.nuggets.controller;

import com.github.nuggets.model.Document;
import com.github.nuggets.model.param.DocumentSaveParams;
import com.github.nuggets.model.pojo.LoginAccountInfo;
import com.github.nuggets.model.result.BaseResult;
import com.github.nuggets.model.result.PageResult;
import com.github.nuggets.service.DocumentService;
import com.github.nuggets.util.LoginAccountUtils;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.*;

/**
 * @description:
 * @author: wangsj
 * @date: 2022/3/22 5:45 下午
 */
@RestController
@RequestMapping("/nuggets")
public class DocumentController {

    @Autowired
    private DocumentService documentService;


    @Operation(summary = "发布文章", description = "发布文章")
    @PostMapping("/document")
    public BaseResult<Boolean> releaseDocument(OAuth2Authentication auth, @RequestBody DocumentSaveParams params) {
        LoginAccountInfo loginAccount = LoginAccountUtils.getLoginAccount(auth);
        return BaseResult.success(documentService.releaseDocument(loginAccount, params));
    }


    @Operation(summary = "发布文章", description = "发布文章")
    @DeleteMapping("/document/{documentId}")
    public BaseResult<Boolean> deleteDocument(OAuth2Authentication auth, @PathVariable Long documentId) {
        LoginAccountInfo loginAccount = LoginAccountUtils.getLoginAccount(auth);
        return BaseResult.success(documentService.deleteDocument(loginAccount, documentId));
    }


    @Operation(summary = "修改文章", description = "修改文章")
    @PostMapping("/document/{documentId}")
    public BaseResult<Boolean> updateDocument(OAuth2Authentication auth, @RequestBody DocumentSaveParams params, @PathVariable Long documentId) {
        LoginAccountInfo loginAccount = LoginAccountUtils.getLoginAccount(auth);
        return BaseResult.success(documentService.updateDocument(loginAccount, documentId, params));
    }

    @Operation(summary = "修改文章", description = "修改文章")
    @GetMapping("/document/{documentId}")
    public BaseResult<Document> getDocument(OAuth2Authentication auth, @PathVariable Long documentId) {
        LoginAccountInfo loginAccount = LoginAccountUtils.getLoginAccount(auth);
        return BaseResult.success(documentService.getLoginAccountDocument(loginAccount, documentId));
    }

    @Operation(summary = "修改文章", description = "修改文章")
    @GetMapping("/document")
    public BaseResult<PageResult<Document>> listDocument(OAuth2Authentication auth,
                                                         @RequestParam(value = "state", required = false) Integer state,
                                                         @RequestParam("pageIndex") Integer pageIndex,
                                                         @RequestParam("pageSize") Integer pageSize) {
        LoginAccountInfo loginAccount = LoginAccountUtils.getLoginAccount(auth);
        return BaseResult.success(documentService.listLoginAccountDocument(loginAccount, state, pageIndex, pageSize));
    }


}
